package org.example.day06.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.example.day06.util.JDBCUtil;

import javax.sql.DataSource;
import javax.xml.crypto.Data;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JobInfoDaoImpl implements IJobInfoDao{

    @Override
    public Object getCenter2Data() {
        DataSource dataSource= JDBCUtil.getDataSource();
        QueryRunner runner=new QueryRunner(dataSource);
        String sql="select 省份, count(*) from job_info group by 省份";
        List<Map<String, Object>> lists=null;
        List<Map<String ,Object>> new_data=new ArrayList<>();
        //{"name": '河南', "value": 356} {省份=浙江, count(*)=610}
        try {
            lists= runner.query(sql, new MapListHandler());
            for(Map<String,Object> map:lists){
                Map<String,Object> new_map=new HashMap<>();
                new_map.put("name",map.get("省份"));
                new_map.put("value",map.get("count(*)"));
                new_data.add(new_map);
            }
            System.out.println(new_data);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return new_data;
    }
}
